<?php 
include '../config.php';

/*
 https://elixir.omnimkt.com/elixir07/web/order/?c=cron-order
*/
function http_post_json($url, $jsonStr)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json; charset=utf-8',
        'Content-Length: ' . strlen($jsonStr)
    )
        );
    $response = curl_exec($ch);
    var_dump($response);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    return $response;
}

$mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
if (!$mysqli->set_charset("utf8")) {}

$query=$mysqli->query("SELECT * FROM `orders`   WHERE `isshot` = 0 limit 0,1");
if($query->num_rows>0){
    $access_token=getToken();
    $url='https://api.weixin.qq.com/cgi-bin/express/business/order/add?access_token='.$access_token;
    $getOrder=$query->fetch_array();
    $getId=$getOrder['id'];
    $order_id=$getOrder['order_id'];
    $openid=$getOrder['openid'];
    echo $order_id;
    echo "<br />";
    echo $openid;
    echo "<br />";
    $queryUser=$mysqli->query("SELECT * FROM `uinfo` WHERE `openid` ='".$openid."' limit 0,1");
    if($queryUser->num_rows>0){
        $userInfo=$queryUser->fetch_array();
        if (empty($userInfo['memberno']) || empty($userInfo['username']) || empty($userInfo['phone']) || empty($userInfo['sheng'])){
            
            echo "update orders set isshot=7 where id='".$getId."'   ";
            
            $mysqli->query("update orders set isshot=7 where id='".$getId."'   ");
            $mysqli->close();
            echo '
        <script>
        window.setTimeout(function(){
        	window.location.reload();
        }, 1000);
        </script>
        ';
            exit;
        }
        echo "<br />";
        echo $userInfo['isold'];
        echo "<br />";
        if ('old' == $userInfo['isold']){
            $mysqli->query("update orders set isshot=9 where id='".$getId."'   ");
            $mysqli->close();
            echo 'update old';
            echo '
        <script>
        window.setTimeout(function(){
        	window.location.reload();
        }, 1000);
        </script>
        ';
            exit;
            
        }
        echo $userInfo['username'],':',$userInfo['phone'],':',$userInfo['sheng'],':',$userInfo['shi'];
    }else{
        exit;
    }
    echo "<br /><br />";

    $orderInfo=array(
        'add_source'=>0,
        'order_id'=> $order_id,
        'openid'=>$openid,
        'delivery_id'=> 'YUNDA',
        'biz_id'=> '2010225552',
        'sender'=>array(
            'name'=>'怡丽丝尔',
            'tel'=>'021-39291306',
            'company'=>'ogilvy',
            'post_code'=> '200001',
            'country'=>'中国',
            'province'=>'上海市',
            'city'=>'上海市',
            'area'=>'青浦区',
            'address'=>'胜利路1588号'
        ),
        'receiver'=>array(
            'name'=>$userInfo['username'],
            'mobile'=>$userInfo['phone'],
            'country'=>'中国',
            'province'=>$userInfo['sheng'],
            'city'=>$userInfo['shi'],
            'area'=>$userInfo['shi'],
            'address'=>$userInfo['dizhi']
        ),
        'shop'=>array(
            'wxa_path'=>'/pages/index/index',
            'img_url'=>'https://elixir.omnimkt.com/elixir07/img/shop.jpg',
            'goods_name'=>'墨迹天气小程序GWP',
            'goods_count'=>1
        ),
        'cargo'=>array(
            'count'=>1,
            'weight'=>1,
            'space_x'=>20,
            'space_y'=>20,
            'space_z'=>20,
            'detail_list'=>array(
                array(
                    'name'=>'MJTQ20200709',
                    'count'=>1
                )
            )
        ),
        'insured'=>array(
            'use_insured'=>0,
            'insured_value'=>0
        ),
        'service'=>array(
            'service_type'=>0,
            'service_name'=>'标准快递'
        )
        
    );
    
    $jsonInfo=json_encode($orderInfo,  JSON_UNESCAPED_UNICODE);
    
    echo $jsonInfo;
    echo "<br /><br />";
    
    $returnMsg=http_post_json($url, $jsonInfo);
     
    $getData=json_decode($returnMsg, true);
    var_dump($getData);
    
    $mysqli->query("insert into orders_log(openid,order_id,getmsg,createtime) values('".$openid."','".$order_id."','".$returnMsg."','".date("Y-m-d H:i:s")."')  ");
  
    if (isset($getData['waybill_id'])){
        $mysqli->query("update orders set waybill_id='".$getData['waybill_id']."', isshot=1,createtime='".date("Y-m-d H:i:s")."' where id='".$getId."'   ");
    }else{
        $mysqli->query("update orders set isshot=7 where id='".$getId."'   ");
    }
    $mysqli->close();
    
    $rand=rand(1,2);
    if(1 == $rand){
        $time=900;
    }else{
        $time=500;
    }
    
    echo '
        <script>
        window.setTimeout(function(){
        	window.location.reload();
        }, '.$time.');
        </script>
        ';
  
    exit;
    
}else{
    echo 'no order';
}


exit;
 

$theorderInfo=json_decode($orderInfo, true);
$jsonInfo=json_encode($theorderInfo,  JSON_UNESCAPED_UNICODE);
 
echo $jsonInfo;
echo "<br /><br />"; 
$getdata=json_decode(http_post_json($url, $jsonInfo),true);
var_dump($getdata);




?>

